<template>
  <div id="asn" v-loading="loading">
    <v-tableTitle
      :btnConfig="btnConfig"
      :title="$t('in.beaCodePrint')"
      @newBeacode="newBeacode"
      @editBeacode="editBeacode"
    ></v-tableTitle>
    <v-table
      :pageSize="pageSize"
      :page="page"
      :tableConfig="tableConfig"
      :tableData="tableData"
      :total="total"
      :hasEdit="false"
      @findData="getTableData"
      @getTableData="getTableData"
      @searhData="searhData"
      @selectData="selectData"
      @upData="getTableData"
      ref="table"
    ></v-table>

    <el-dialog
      :title="$t('in.codePrint')"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :show-close="false"
      width="20%"
    >
      <el-row>
        <el-col :span="6" style="height:40px;line-height: 40px">
          {{ $t("in.codePrintNum") }}:
        </el-col>
        <el-col :span="18">
          <el-input
            v-model="numberno"
            :disabled="disabled"
            placeholder=""
          ></el-input>
        </el-col>
      </el-row>

      <span slot="footer">
        <el-button
          type="primary"
         
          :disabled="!this.btnPermNameList.includes('WM010903')"
          @click="defineClick"
          >{{$t('common.confirm')}}</el-button
        >
        <el-button
         
          :disabled="!this.btnPermNameList.includes('WM010904')"
          @click="dialogVisible = false"
          >{{$t('common.cancel')}}</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script type="text/ecmascript-6">
import tableTitle from "commons/TableTitle";
import table from "commons/Table";
import baseMixin from "@/mixins/BaseMixin";
import config from "./Config";
import bus from "../../../../bus/bus";
import print from '../../../../assets/js/print';
export default {
  name: "barcodeprint",
  mixins: [baseMixin, config],
  data () {
    return {
      loading: false,
      tableConfig: [
        {
          label: this.$t('in.labelNo'),
          prop: "id",
          input: "input",
          disabled: false
        },
        {
          label: this.$t('in.receiptkey1'),
          prop: "receiptkey",
          input: "input",
          disabled: false
        },
        {
          label: this.$t('in.stuffCode'),
          prop: "sku",
          input: "input",
          disabled: false
        }, {
          label: this.$t('in.stuffName'),
          prop: "descr",
          input: "input",
          disabled: false
        }, {
          label: this.$t('in.editdate'),
          prop: "editdate",
          input: "date",
          disabled: false
        },
      ],
      tableData: [],
      pageSize: 10,
      page: 1,
      total: 0,
      tableSearhData: {},
      tableSelectData: [],
      dialogVisible: false,
      numberno: null,
      numbernoType: '',
      disabled: false
    };
  },
  computed: {
    btnConfig () {
      return [
        { label: this.$t('in.newBeacode'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM010901'), invalid: "newBeacode" },
        { label: this.$t('in.editBeacode'), certigier: this.deleteStatus||!this.btnPermNameList.includes('WM010902'), invalid: "editBeacode" },
      ]
    }
  },
  created () {
  },
  methods: {
    //确定打印条码的数量
    defineClick () {
      let reg = /^\+?[1-9]\d*$/;//大于零的正整数
      if (reg.test(this.numberno)) {
        let params = {}
        if (this.tableSelectData.length === 1) {
          params = this.tableSelectData[0]
        }
        this.$api.post(`/printBarCode/` + this.numberno + '/' + this.numbernoType, params).then(res => {
          
            this.$message.success(this.$t('app.saveMes'));
            this.dialogVisible = false;
            this.getTableData(1, this.tableSearhData);
            for(let i = 0; i < res.data.list.length;i++){
              print.barcodeprint(res.data.list[i].id);
            }
        });
      } else {
        this.$message.error(this.$t('app.inputGreaterZeroNumberPlease'));
        this.numberno = null
      }

    },
    //选中的内容
    selectData (data) {
      this.tableSelectData = data;
    },
    //打印新条码
    newBeacode () {
      this.numbernoType = 'new'
      this.disabled = false
      this.dialogVisible = true
    },
    //补印新条码
    editBeacode () {
      this.numbernoType = 'old'
      this.numberno = 1
      this.disabled = true
      this.dialogVisible = true
    },
    async getTableData (page, data) {
      try {
        this.loading = true;
        let res = await this.$api.post(`/queryBarCodePrintList/${page}`, data);
        
          this.tableData = res.page.list;
          this.page = res.page.curPage;
          this.pageSize = res.page.pageSize;
          this.total = res.page.total;
      } catch (e) {
        this.$message.error(e);
      } finally {
        this.loading = false;
      }
    },
    //搜索的值
    searhData (data, up) {
      if (!up) {
        this.tableSearhData = data;
      } else {
        this.tableSearhData = data;
        this.getTableData(1, this.tableSearhData);
      }
    },

  },
  mounted () {
    
    this.$store.commit("setSkip", {});
    this.$store.commit("setRouteId", '');
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", {});
    this.$store.commit("setTemporary", "")
    this.getTableData(1, {});
  },
  watch: {
    tableSelectData (data) {
      if (this.permissionStatus) {
        if (data.length >= 2) {
          this.$message.error(this.$t('msg.msg32'));
        }
        if (data.length === 1) {
          this.deleteStatus = this.downloadStatus = this.actionStatus = this.blindActionStatus = false;
        } else {
          this.deleteStatus = this.downloadStatus = this.actionStatus = this.blindActionStatus = true;
        }
      }
    }
  },
  components: {
    "v-tableTitle": tableTitle,
    "v-table": table,
  }
};
</script>

<style scoped>
#asn {
  width: 100%;
  height: 99%;
  padding-top: 6px;
}
</style>
